﻿using PSI.Filters;
using PSI.Helper;
using PSI.Models;
using PSI.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace PSI.Controllers {
    public class HomeController : PSIController {
        private UserService UserService { get { return new UserService(); } }

        public ActionResult Index() {
            ViewBag.UserName = UserService.GetLoginUserName();

            return View();
        }

        [HttpPost]
        public ActionResult GetMainMenuItems() {
            var root = new MainMenuService().BuildMenuItemTree(false);

            return Json(root);
        }

        public ActionResult NavigateTo(string fid) {
            new FIdService().InsertRecentFId(fid);

            new LogService().Log("进入模块:" + new MainMenuService().GetFidCaption(fid));

            //
            if (fid.Length == 4 && fid.StartsWith("2")) {

                return Redirect("/BaseCodeRuntime/Index?fid=" + fid);
            }

            switch (fid) {
                case FIdConst.RE_LOGIN:
                    // 重新登录
                    new LogService().Log("退出系统(重新登录功能)");
                    UserService.ClearLoginUser();
                    return Redirect("/");
               
                case FIdConst.CHANGE_MY_PASSWORD:
                    // 修改我的密码
                    return Redirect("/User/ChangeMyPassword/Index");
                case FIdConst.USER:
                    // 用户管理
                    return Redirect("/User/Index");
                case FIdConst.PERMISSION:
                    // 权限管理
                    return Redirect("/Permission/Index");
                case FIdConst.LOG:
                    // 系统日志
                    return Redirect("/Log/Index");
                case FIdConst.BASE_CODE:
                    // 码表结构管理
                    return Redirect("/BaseCodeAdmin/Index");
                case FIdConst.MENU_MASSAGE:
                    // 菜单管理
                    return Redirect("/MainMenuAdmin/Index");
                case FIdConst.PROP_ACCOUNT:
                    // 菜单管理
                    return Redirect("/AccountTitleAdmin/Index");
                default:
                    return View();
            }
        }

        public ActionResult GetImage(string id) {
            var fileName = Server.MapPath("~/Images/fid/fid" + id + ".png");
            if (!System.IO.File.Exists(fileName)) {
                fileName = Server.MapPath("~/Images/fid/default.png");
            }

            return File(fileName, "image/png");
        }

        [HttpPost]
        public ActionResult GetRecentFid() {
            var data = new FIdService().GetRecentFid();

            return Json(data);
        }
    }
}
